package com.czy.owner.ui.accountbalance;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.api.AccontInfoApi;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.AccountInfoModel;
import com.czy.owner.global.Constants;
import com.czy.owner.net.api.BaseResultEntity;
import com.czy.owner.net.http.HttpManager;
import com.czy.owner.net.listener.HttpOnNextListener;
import com.czy.owner.rxbus.RxBus;
import com.czy.owner.rxbus.Subscribe;
import com.czy.owner.rxbus.ThreadMode;
import com.czy.owner.rxbus.event.CommonEvent;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.utils.SharedPreferencesUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by wangyang on 2016/10/8 0008.
 * 账户余额
 */
public class AccountBalanceActivity extends BaseActivity {
    @BindView(R.id.tv_acoount_bannce)
    TextView tvAcoountBannce;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_account_balance;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText(getString(R.string.person_store_bannce));
        RxBus.getDefault().register(this);
        accountInfo();
    }

    @Override
    protected void updateViews() {

    }


    @OnClick(R.id.btn_now_recharge)
    public void nowRechange(View view) {
        Intent ti = new Intent(AccountBalanceActivity.this, RechargeActivity.class);
        startActivity(ti);
    }

    @OnClick(R.id.btn_transaction_record)
    public void transactionRecord(View view) {
        Intent ti = new Intent(AccountBalanceActivity.this, TransactionRecordActivity.class);
        startActivity(ti);
    }



    @Subscribe(code = Constants.RECHARGE_SUCCESS_CODE, threadMode = ThreadMode.MAIN)
    public void eventRecharge(CommonEvent rechargeEvent) {
        accountInfo();
    }

    //用户账户信息Api
    private void accountInfo() {
        AccontInfoApi accontInfoApi = new AccontInfoApi(new HttpOnNextListener<AccountInfoModel>() {
            @Override
            public void onNext(AccountInfoModel model) {
                MyLog.e("yang", "账户信息==" + model.toString());
                setAccountInfo(model);
            }

            @Override
            public void onCacheNext(String cache) {
                super.onCacheNext(cache);
                Gson gson = new Gson();
                Type type = new TypeToken<BaseResultEntity<AccountInfoModel>>() {
                }.getType();
                BaseResultEntity<AccountInfoModel> accountInfoCache = gson.fromJson(cache, type);
                setAccountInfo(accountInfoCache.getData());
            }


            @Override
            public void onError(Throwable e) {
                super.onError(e);
                PhoneUtils.ShowToastMessage(AccountBalanceActivity.this, e.getMessage());
            }
        }, this);

        accontInfoApi.setSession(UserHelper.getInstance().getSessionInfoModel(AccountBalanceActivity.this).getSession());
        //accontInfoApi.setStoreId(SharedPreferencesUtils.getParam(AccountBalanceActivity.this, UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_BINDSTOREID, 0) + "");
        HttpManager manager = HttpManager.getInstance();
        manager.doHttpDeal(accontInfoApi);

    }

    private void setAccountInfo(AccountInfoModel model) {
        double storeBalance = model.getStoreBalance();
        tvAcoountBannce.setText("￥" + String.format("%.2f", storeBalance));

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxBus.getDefault().unRegister(this);
    }
}
